package indra;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Date;

public class LeerArchivo {
	public static void main(String[] arg) {
		System.out.println("Inicio" + (new Date()));

		FileInputStream fileInputStream;
		InputStreamReader inputStreamReader;
		BufferedReader bufferedReader;
		
		FileOutputStream fileOutputStream;
		OutputStreamWriter outputStreamWrite;
		BufferedWriter bufferWrite;
		
		File directorio = new File("C:\\data\\postgres\\migrar\\csv");
		String linea;
		
		try {
			for (File f : directorio.listFiles()) {
				System.out.println(f.getName());
				
				fileInputStream = new FileInputStream(f);
				inputStreamReader = new InputStreamReader(fileInputStream);
				bufferedReader = new BufferedReader(inputStreamReader);
				
				fileOutputStream = new FileOutputStream("utf8_" + f.getName().toLowerCase());
				outputStreamWrite = new OutputStreamWriter(fileOutputStream, "utf-8");
				bufferWrite = new BufferedWriter(outputStreamWrite);
				
				
				while ((linea = bufferedReader.readLine())!=null) {
				  bufferWrite.write(linea);
				  bufferWrite.newLine();
				}
				
				bufferedReader.close();
				inputStreamReader.close();
				fileInputStream.close();
				
				bufferWrite.close();
				outputStreamWrite.close();
				fileOutputStream.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		System.out.println("Termino" + (new Date()));
	}
}
